﻿/**
 * ...
 * @author Lobanov A.V.
 */

package view.module
{
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import view.components.mainMenu.Logotype;
	import view.components.mainMenu.MainMenu;
	
	import flash.display.MovieClip;
	
	/**
	 * A Mediator for interacting with the ...
	 */
	public class MainMenuMediator extends Mediator implements IMediator 
	{
		
		// Cannonical name of the Mediator
		public static const NAME:String = "MainMenuMediator";
		
		private var _logotype:Logotype;
		private var _mainMenu:MainMenu;
		
		private var _viewComponent:MovieClip;
		
		public function MainMenuMediator( viewComponent:Object ) 
		{
			// pass the viewComponent to the superclass where 
			// it will be stored in the inherited viewComponent property
			super( NAME, viewComponent );
			
			_viewComponent = viewComponent as MovieClip;
		}
		
		private function initViewComponents($xmlData:XML):void 
		{
			_mainMenu = new MainMenu($xmlData.pages);
			_logotype = new Logotype();			
			_viewComponent.addChild(_logotype);
			_viewComponent.addChild(_mainMenu);
		}
        
		/**
		 * List all notifications this Mediator is interested in.
		 * <P>
		 * Automatically called by the framework when the mediator
		 * is registered with the view.</P>
		 * 
		 * @return Array the list of Nofitication names
		 */
		override public function listNotificationInterests():Array 
		{
			return [ApplicationFacade.STAGE_IS_RESIZE,
					ApplicationFacade.CORE_DATA_IS_LOAD];
		}
		
		/**
		 * Handle all notifications this Mediator is interested in.
		 * <P>
		 * Called by the framework when a notification is sent that
		 * this mediator expressed an interest in when registered
		 * (see <code>listNotificationInterests</code>.</P>
		 * 
		 * @param INotification a notification 
		 */
		override public function handleNotification( note:INotification ):void 
		{
			switch ( note.getName() ) 
			{           
				case ApplicationFacade.STAGE_IS_RESIZE:
					_logotype.setPosition(
						(note.getBody() as Array)[0] as Number, 
						(note.getBody() as Array)[1] as Number
					);
					
					_mainMenu.setPosition(
						(note.getBody() as Array)[0] as Number, 
						(note.getBody() as Array)[1] as Number
					);
				break;
				
				case ApplicationFacade.CORE_DATA_IS_LOAD:
					initViewComponents(note.getBody() as XML);
				break;
				
				default:
					break;		
			}
		}
	}
}